<?php

class FormForgotpass extends CFormModel{
    public $email;
    
    public $verifyCode;
    
    private $_identity;
    
    public $last_error = '';
    
    public function rules(){
        return array(
            array('email, verifyCode', 'required'),
            array('email', 'email', 'message'=>'Неверный формат E-mail'),
            array('verifyCode', 'captcha', 'allowEmpty' => !extension_loaded('gd'), 'captchaAction' => 'site/captcha'),
        );
    }
    
    public function attributeLabels(){
        return array(
            'email'=>UserModule::t('fields', 'Email'),
            'verifyCode'=>UserModule::t('fields', 'Капча')
        );
    }
    
    public function sendRequest(){
        
        $USER = Users::model()->findByAttributes(array(
            'email' => $this->email,
        ));
        if(count($USER) <= 0){
            $this->addError('email', Yii::t('errors', 'User with this email was not found'));
        }else{
            
            $USER->checkword = md5(rand().PASSWORD_SECRET2.rand().PASSWORD_SECRET2.time());
            
            $USER->save();
            
            $message = new YiiMailMessage;
            $message->setSubject(Yii::app()->name.'# Смена пароля');
            $message->view = 'modules.user.views.mail.forgotpass_request';
            $message->setBody(array(
                'email' => $USER->email,
                'checkword' => $USER->checkword,
            ), 'text/html');
            
            $message->addTo($USER->email);
            $message->setFrom(Yii::app()->getController()->GetModule()->emailSender);
            Yii::app()->mail->send($message);
            
        }
        
    }
    
    public function makeNewPass($USER, $checkword){
        
        $newpass = self::randomString(10);
        
        if($checkword != $USER->checkword){
            $this->last_error = 'Неверная контрольная строка.';
        }else{
            
            $USER->checkword = '';
            $USER->password = Users::CryptPassword($newpass);
            
            if($USER->save()){
                $message = new YiiMailMessage;
                $message->setSubject(Yii::app()->name.'# Новый пароль');
                $message->view = 'modules.user.views.mail.forgotpass_newpass';
                $message->setBody(array(
                    'newpass' => $newpass,
                ), 'text/html');
                
                $message->addTo($USER->email);
                $message->setFrom(Yii::app()->getController()->getModule()->emailSender);
                Yii::app()->mail->send($message);
                
                return true;
            }
            
        }
        
        return false;
    }
    
    public static function randomString($length=10) {
        $characters = '0123456789abcdefghijklmnopqrstuvwxyz';
        $string = '';
        for ($p=0;$p<$length;$p++) {
            $string .= $characters[mt_rand(0, strlen($characters)-1)];
        }
        return $string;
    }
}
